<?php
/**
 * Description:
 * Author: hezhizheng
 * Date: 2018/7/16
 * Time: 11:05
 * Created by Created by Panxsoft.
 */

namespace App\Services\Youzan;

use App\Models\Customer\Customer;
use App\Models\Panx\Product;
use App\Models\Panx\ProductUrlType;
use App\Models\Store\Store;
use Illuminate\Support\Carbon;

class ProductService
{

    /**
     * @param array $items
     * @param Customer $customer
     * @param null|Store $store
     * @return array
     */
    public function transformItems(array $items,
                                   Customer $customer,
                                   Store $store = null): array
    {
        $product_params = [];

        // 这里有个问题，如果之前有同步产品，是否所有的属性都应该覆盖掉？ 现在 spu_code 每次点同步都会变
        $start_time = time();

        foreach ($items as $item) {
            $spu_code = $start_time-- .  ($item['item_no'] ?? ''); // 防止spu_code 重复

            $product_params[] = [
                'youzan_item_id' => $item['item_id'],
                'name'           => $item['title'],
                'title'          => $item['title'],
                'description'    => $item['title'],
                'customer_id'    => $customer->id,
                'store_id'       => $store->id ?? Product::NO_STORE_ID,
                'market_price'   => (float) $item['origin'], // 有赞划线价单位是元
                'sell_price'     => ((float) $item['price']) / 100,
                'stock'          => $item['quantity'],
                'is_deleted'     => Product::IS_NO_DELETE,
                'is_sale'        => Product::IS_NO_SALE,
                'priority'       => $item['num'],
                'spu_code'       => $spu_code,
                'created_at'     => Carbon::now(),
                'updated_at'     => Carbon::now(),
                'image'          => $item['image'],  // 商品的图片信息

                'product_url_type_id' => ProductUrlType::ID_YOUZAN,
                // 'product_url' => '', 此时还没有产生 product_id、不知道device_account_id
            ];
        }

        return $product_params;
    }
}